package event;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class Example_6_5_JRadioButton_2 extends JFrame {

	private static final long serialVersionUID = 1L;
    private JPanel buttonPane;
    private ButtonGroup group;
    private JLabel label;
    private static final int DEFAULT_SIZE = 12;
    
    public Example_6_5_JRadioButton_2() {
    	super("显示单选按钮JRadioButton");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 400, 260);
		this.getContentPane().setLayout(new FlowLayout());
		
     
        label = new JLabel("显示单选按钮JRadioButton！！！");
        label.setFont(new Font("楷体", Font.PLAIN, DEFAULT_SIZE));
        this.getContentPane().add(label, BorderLayout.CENTER);
        
        group = new ButtonGroup();
        buttonPane = new JPanel();
        
        addBtn("Small", 8, Color.BLUE);
        addBtn("Medium", 12, Color.GREEN);
        addBtn("Large", 18, Color.YELLOW);
        addBtn("Extra Large", 24, Color.RED);
        
        this.getContentPane().add(buttonPane, BorderLayout.SOUTH);
		this.setVisible(true);
    }
    
    public void addBtn(String name, final int size, final Color color) {
        boolean selected = (size == DEFAULT_SIZE);
        
        JRadioButton newBtn = new JRadioButton(name, selected);
        group.add(newBtn);
        buttonPane.add(newBtn);
        
        //this listener sets the label font size
        
        ActionListener listener = new ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    // size refers to final parameter of the addRadioButton
                    //methed
                    label.setFont(new Font("宋体", Font.PLAIN, size));
                    label.setForeground(color);
                }
        };
        
        newBtn.addActionListener(listener);
    }

	public static void main(String[] args) {
		new Example_6_5_JRadioButton_2();
	}
}
